// Try Catch block to test if jQuery loaded.
try{

$(document).ready( function() {

jQuery.extend({
    monthNames: function( value ) {
        var ret = ['JAN','FEB','MAR','APR','MAY','JUN',
                   'JUL','AUG','SEP','OCT','NOV','DEC'];
        if ( value || value === 0 ){
            value -= 1;
            ret = ( ret[value] ) ? ret[value] : undefined;
        }
        return ret;
    },
    is_24hour: function( value ) {
        ret = undefined;
        if ( value ){
            var re = '^[0-1]{1}[0-9]{1}[0-5]{1}[0-9]{1}$' +
                     '|^[2]{1}[0-3]{1}[0-5]{1}[0-9]{1}$';
            ret = value.match(new RegExp(re)) ? true : false;
        }
        return ret;
    },
    is_d2m3y4: function( value ) {
        ret = undefined;
        if ( value ){
            value = value.replace(/ /gi,"").toUpperCase();
            var day = value.substr(0,2);
            var month = jQuery.inArray(value.substr(2,3),
                                       jQuery.monthNames());
            var year = value.substr(5);
            var d = new Date();
            d.setFullYear(year,month,day);
            ret = (
                    year.length != 4 ||
                    d.getDate() != day ||
                    d.getMonth() != month ||
                    d.getFullYear() != year) ? false : true;
        }
        return ret;
    },
    d2m3y4_to_y4m2d2: function( value ) {
        ret = undefined;
        if ( value ){
            value = value.replace(/ /gi,"").toUpperCase();
            var day = value.substr(0,2);
            day = ( (day.length < 2) ? '0' + day : day );
            var month = String(jQuery.inArray(value.substr(2,3),
                                              jQuery.monthNames()) + 1);
            month = ( (month.length < 2) ? '0' + month : month );
            var year = value.substr(5,4);
            ret = year + '-' + month + '-' + day;
        }
        return ret;
    },
    y4m2d2_to_d2m3y4: function( value ) {
        ret = undefined;
        if ( value ){
            var day = value.substr(8,2);
            day = ( (day.length < 2) ? '0' + day : day );
            var month = value.substr(5,2);
            month = jQuery.monthNames(month);
            var year = value.substr(0,4);
            ret = day + ' ' + month + ' ' + year;
        }
        return ret;
    },
    qs: function( key, value ) {
        var ret = {};
        var q = location.search;
        q = q.replace(/^\?/,'');
        q = q.replace(/\&$/,'');
        q = q.replace(/\+/g,' ');
        jQuery.each(q.split('&'), function() {
            var k = this.split('=')[0];
            ret[k] = [];
        });
        jQuery.each(q.split('&'), function() {
            var k = this.split('=')[0];
            var v = this.split('=')[1];
            v = decodeURIComponent(v);
            ret[k].push(v);
        });
        jQuery.each(ret, function(k, v) {
            if ( !v[1] && v[1] !== '' ){
                ret[k] = v[0];
            }
        });
        if ( key ){
            ret = ret[key] ? ret[key] : ( 
                ( value || value === '' ) ? value : undefined
            );
        }
        return ret;
    },
    metaMonth: function( value ) {
        var url = '/api/dates/month/' +
                  ( value ? value + '/' : '' );
        return metaMaker(url);         
    },
    metaDay: function( value ) {
        var url = '/api/dates/day/' +
                  ( value ? value + '/' : '' );
        return metaMaker(url);      
    },
    logMessage: function( msg, level ) {
        var url = '/api/logger/';
        var data = {message: msg, level: 'DEBUG'};
        if ( level ){
            data.level = level;
        }
        $.post(url, data);
    }
});

function metaMaker( url ) {
    var ret = '';
    $.ajax({
        type: "GET",
        url: url,
        dataType: "json",
        async: false,
        success: function(json){
            ret = json;
        },
        error: function(){
            ret = undefined;
        }
    });
    return ret;
}

});

// Alert a message if jQuery isn't loaded.
}catch(err){ 
    alert('jQuery did not load.');
}